package com.egame.manage.biz.server.gc;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import cn.egame.common.data.BaseDao;
import cn.egame.common.data.SqlUtils;
import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.util.Utils;
import cn.egame.interfaces.gc.TerminalRecognitionInfo;

public class TerminalRecoginitionServiceDao extends BaseDao {
	
	private static final String STR_SELECT_TERMINALRECOGNITIN_INFO = "api_code, ua_code, screen_px, terminal_id, is_typical, remark, operator_id, insert_time, update_time,game_type";
	
	public TerminalRecoginitionServiceDao()
			throws ExceptionCommonBase {
		super("egame_ext");
	}
	
	//api_code, ua_code, screen_px, terminal_id, is_typical, remark, operator_id, insert_time, update_time
	public int updateTerminalRecognitionInfo(TerminalRecognitionInfo bean) throws ExceptionCommonBase {
		if(bean != null){
			String sql = null;
			if(bean.getIsTypical() == 1){				
				if(bean.getIsTypical() == 1){
					sql = "update t_admin_terminal_recognition set is_typical = 0";
					this.executeUpdate(sql);
				}
			}
			sql = "update t_admin_terminal_recognition set "
					+ " terminal_id = " + bean.getTerminalId() + ","
					+ " is_typical = " + bean.getIsTypical() + ","
					+ " remark = " + SqlUtils.QuataStr(bean.getRemark()) + ","
					+ " operator_id = " + bean.getOperatorId() + ","
					+ " update_time = now()"
					+ " where api_code = " + SqlUtils.QuataStr(bean.getApiCode())
					+ " and ua_code = " + SqlUtils.QuataStr(bean.getUaCode())
					+ " and screen_px = " + SqlUtils.QuataStr(bean.getScreenPx());
			System.out.println(sql);
			return this.executeUpdate(sql);
		}
		return -1;
	}
	
	public int deleteTerminalRecognitionInfo(String apiCode, String uaCode, String screenPx) throws ExceptionCommonBase {
		String sql = "delete from t_admin_terminal_recognition"
				+ " where api_code = " + SqlUtils.QuataStr(apiCode)
				+ " and ua_code = " + SqlUtils.QuataStr(uaCode)
				+ " and screen_px = " + SqlUtils.QuataStr(screenPx);
		System.out.println(sql);
		return this.executeUpdate(sql);
	}

	public int deleteTerminalRecognitionInfo(int terminalId) throws ExceptionCommonBase {
		String sql = "delete from t_admin_terminal_recognition"
				+ " where terminal_id = " + terminalId;
		System.out.println(sql);
		return this.executeUpdate(sql);
	}
	
	public int setTerminalRecognitionInfo(TerminalRecognitionInfo bean) throws ExceptionCommonBase {
		if(bean != null){
			String sql = null;
			if(bean.getIsTypical() == 1){
				sql = "update t_admin_terminal_recognition set is_typical = 0";
				this.executeUpdate(sql);
			}
			sql = "insert into t_admin_terminal_recognition(" + STR_SELECT_TERMINALRECOGNITIN_INFO + ") values (" 
					+ SqlUtils.QuataStr(bean.getApiCode()) + "," 
					+ SqlUtils.QuataStr(bean.getUaCode()) + ","
					+ SqlUtils.QuataStr(bean.getScreenPx()) + ","
					+ bean.getTerminalId() + ","
					+ bean.getIsTypical() + ","
					+ SqlUtils.QuataStr(bean.getRemark()==null?"":bean.getRemark()) + ","
					+ bean.getOperatorId() + ","
					+ " now(), now()," + bean.getGameType().value() + ")";
			return this.executeUpdate(sql);
		}
		return -1;
	}
	
	public int getTypecalTerminalId()throws ExceptionCommonBase{
		String sql = "select terminal_id from t_admin_terminal_recognition where is_typical = 1";
		return this.getInt(sql);
	}
	
	public List<String[]> listTerminalRecognition(TerminalRecognitionInfo bean,int manufactrueId,int page,int rowsOfPage)throws RemoteException{
		String sql = "select api_code,ua_code,screen_px from t_admin_terminal_recognition where 1=1 ";
		if(bean != null){
			if(bean.getTerminalId()>0){
				sql += " and terminal_id = " + bean.getTerminalId();
			}
		}
		if(manufactrueId>0){
			List<Integer> terminalIds = TerminalServiceImpl.getInstance().listTerminalIds(manufactrueId);
			if(terminalIds == null || terminalIds.size()==0){
				terminalIds = new ArrayList<Integer>();
				terminalIds.add(0);
			}
			sql += " and terminal_id in (" + Utils.toString(terminalIds) + ")";
		}
		sql += " limit " + page * rowsOfPage + "," + rowsOfPage;
		System.out.println(sql);
		return this.getListStrings(sql);
	}
	
	public int getTerminalRecognitionCount(TerminalRecognitionInfo bean,int manufactrueId)throws RemoteException{
		String sql = "select count(*) from t_admin_terminal_recognition where 1=1 ";
		if(bean != null){
			if(bean.getTerminalId()>0){
				sql += " and terminal_id = " + bean.getTerminalId();
			}
		}
		if(manufactrueId>0){
			List<Integer> terminalIds = TerminalServiceImpl.getInstance().listTerminalIds(manufactrueId);
			if(terminalIds == null || terminalIds.size()==0){
				terminalIds = new ArrayList<Integer>();
				terminalIds.add(0);
			}
			sql += " and terminal_id in (" + Utils.toString(terminalIds) + ")";
		}
		return this.getInt(sql);
	}

}
